/**
 * 字典 cell 状态全局控制中心
 */
import {
    SkuCode
} from "./sku-code.js"

import {
    CellStatus
} from "../../core/enum.js"

import {
    Joiner
} from "../../utils/joner.js"

import {
    SkuPending
} from "./sku-pending.js"


class Judger {
    fenceGroup
    totalPathDict = []
    skuPending
    constructor(fenceGroup) {
        this.fenceGroup = fenceGroup
        this._initSkuPengding()
        this._initPathDict()
    }

    _initSkuPengding() {
        this.skuPending = new SkuPending()
    }

    _initPathDict() {
        this.fenceGroup.skuList.forEach(item => {
            const skuCode = new SkuCode(item.code)
            // 将所有sku的路径连接起来组成字典
            this.totalPathDict = this.totalPathDict.concat(skuCode.codePathArr)
        })
    }

    changeCellStatus(cell, row, col) {
        this._changeCurrentCellStatus(cell, row, col)
        this._changeOtherCellStatus()
    }

    /**
     * 改变当前cell状态
     */
    _changeCurrentCellStatus(cell, x, y) {
        const _cell = this.fenceGroup.fences[x].cells[y]
        if (cell.status === CellStatus.WAITING) {
            _cell.status = CellStatus.SELECTED
            this.skuPending.insertCell(cell, x)
        }
        if (cell.status === CellStatus.SELECTED) {
            _cell.status = CellStatus.WAITING
            this.skuPending.removeCell(x)
        }
    }

    /**
     * 改变other cell 状态
     */
    _changeOtherCellStatus() {
        this.fenceGroup.eachCell((cell, row, col) => {
            let path = this._findPotentialPath(cell, row, col)
            if (!path) {
                return
            }
            const isInDict = this._isInDict(path)
            const _cell = this.fenceGroup.fences[row].cells[col]
            if (isInDict) {
                _cell.status = CellStatus.WAITING
            } else {
                _cell.status = CellStatus.FORBIDDEN
            }
        })
    }

    /**
     * 查找潜在路径 
     */
    _findPotentialPath(cell, row, col) {
        const joiner = new Joiner("#")
        for (let i = 0; i < this.fenceGroup.fences.length; i++) {
            const selected = this.skuPending.findSelectedCellByX(i)

            /**
             * 点击的cell是当前行
             *点击当前行已存在的路径跳过
             * 将code 加入潜在路径
             */
            if (i === row) {
                if (this.skuPending.isSelected(cell, row)) {
                    return
                }
                const cellCode = this._getCellCode(cell.cell)
                joiner.join(cellCode)
            } else {
                /**
                 * 点击的其他行
                 * 查找其他行已选元素
                 */

                if (selected) {
                    const selectedCellCode = this._getCellCode(selected.cell)
                    joiner.join(selectedCellCode)
                }
            }
        }
        return joiner.getStr()
    }

    /**
     * 查询潜在路径是否在字典中
     */
    _isInDict(path) {
        return this.totalPathDict.includes(path)
    }

    _getCellCode(cell) {
        return cell.key_id + '-' + cell.value_id
    }
}

export {
    Judger
}